using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.XPath;

namespace Lightray.Hattrick.ChppLib.EntityTypes
{
    public class DetailedMatch : Match
    {
        #region Properties
        public MatchParticipantStats HomeMatchDeatils { get; private set; }
        public MatchParticipantStats AwayMatchDeatils { get; private set; }
        public DateTime FinishedDate { get; private set; }
        public int PossessionFirstHalfHome { get; private set; }
        public int PossessionFirstHalfAway { get; private set; }
        public int PossessionSecondHalfHome { get; private set; }
        public int PossessionSecondHalfAway { get; private set; }      
        public MatchArenaStats Arena { get; private set; }
        //public IEnumerable<Goal> Goals { get; private set; }
        //public IEnumerable<Booking> Bookings { get; private set; }
        public IList<MatchEvent> Events { get; private set; }        
        #endregion

        protected DetailedMatch() { }

        public static new DetailedMatch FromXml(XDocument matchDeatilsXml)
        {
            HattrickXmlReader reader = new HattrickXmlReader(matchDeatilsXml);
            return new DetailedMatch()
            {
                ID = reader.ReadNumber("MatchID"),
                DataTime = reader.ReadDate("FetchedDate"),
                MatchDate = reader.ReadDate("MatchDate"),
                FinishedDate = reader.ReadDate("FinishedDate"),
                Status = parseMatchStatus(matchDeatilsXml),
                AwayTeamId = reader.ReadNumber("AwayTeamID"),
                HomeTeamId = reader.ReadNumber("HomeTeamID"),
                MatchType = reader.ReadMatchType("MatchType"),
                HomeMatchDeatils = MatchParticipantStats.FromXml(reader.ReadElement("HomeTeam")),
                AwayMatchDeatils = MatchParticipantStats.FromXml(reader.ReadElement("AwayTeam")),
                HomeGoals = reader.ReadNumber("HomeGoals"),
                AwayGoals = reader.ReadNumber("AwayGoals"),
                PossessionFirstHalfHome = reader.ReadNumber("PossessionFirstHalfHome"),
                PossessionFirstHalfAway = reader.ReadNumber("PossessionFirstHalfAway"),
                PossessionSecondHalfHome = reader.ReadNumber("PossessionSecondHalfHome"),
                PossessionSecondHalfAway = reader.ReadNumber("PossessionSecondHalfAway"),

                Arena = MatchArenaStats.FromXml(reader.ReadElement("Arena")),
                //Goals = readScorers(matchDeatilsXml),
                //Bookings = readBookings(matchDeatilsXml),
                Events = MatchEvent.FromEventListNode(reader.ReadElement("EventList")),
            };
        }

        private static DetailedMatch FromLiveMatchNode(XElement liveMatchNode)
        {
            HattrickXmlReader reader = new HattrickXmlReader(liveMatchNode);
            return new DetailedMatch()
            {
                ID = reader.ReadNumber("MatchID"),
                DataTime = reader.ReadDate("FetchedDate"),
                MatchDate = reader.ReadDate("MatchDate"),
                Status =  MatchStatus.Ongoing,

                AwayTeamId = reader.ReadNumber("AwayTeamID"),
                HomeTeamId = reader.ReadNumber("HomeTeamID"),

                HomeGoals = reader.ReadNumber("HomeGoals"),
                AwayGoals = reader.ReadNumber("AwayGoals"),

                // missing but can be assumed attribitues
                //MatchType = MatchType.
                //HomeMatchDeatils = MatchParticipantStats.FromXml(reader.ReadElement("HomeTeam")),
                //AwayMatchDeatils = MatchParticipantStats.FromXml(reader.ReadElement("AwayTeam")),
                //PossessionFirstHalfHome = reader.ReadNumber("PossessionFirstHalfHome"),
                //PossessionFirstHalfAway = reader.ReadNumber("PossessionFirstHalfAway"),
                //PossessionSecondHalfHome = reader.ReadNumber("PossessionSecondHalfHome"),
                //PossessionSecondHalfAway = reader.ReadNumber("PossessionSecondHalfAway"),
                //Arena = MatchArenaStats.FromXml(reader.ReadElement("Arena")),

                //Goals = readScorers(matchDeatilsXml),
                //Bookings = readBookings(matchDeatilsXml),

                Events = MatchEvent.FromEventListNode(reader.ReadElement("EventList")),            
            };
        }        

        public static IEnumerable<DetailedMatch> FromLiveXml(XDocument liveXml)
        {
            HattrickXmlReader reader = new HattrickXmlReader(liveXml);
            DateTime dataTime = reader.ReadDate("FetchedDate");

            List<DetailedMatch> ret = new List<DetailedMatch>();
            foreach (var liveMatchNode in liveXml.Descendants("Match"))
                ret.Add(DetailedMatch.FromLiveMatchNode(liveMatchNode));

            return ret.ToEntityCollection();
        } 

        private static MatchStatus parseMatchStatus(XDocument matchDeatilsXml)
        {
            if (matchDeatilsXml.XPathSelectElement("/HattrickData/Match/HomeTeam/Formation").IsEmpty)
                return MatchStatus.Upcoming;

            if (matchDeatilsXml.XPathSelectElement("/HattrickData/Match/FinishedDate").IsEmpty)
                return MatchStatus .Ongoing;

            return MatchStatus.Finished;           
        }

        //private static IEnumerable<Goal> readScorers(XDocument xml)
        //{
        //    List<Goal> ret = new List<Goal>();
        //    foreach (var goalNode in xml.XPathSelectElements("/HattrickData/Match/Scorers/Goal"))
        //        ret.Add(Goal.FromXml(goalNode));
        //    return ret;
        //}

        //private static IEnumerable<Booking> readBookings(XDocument xml)
        //{
        //    List<Booking> ret = new List<Booking>();
        //    foreach (var bookingNode in xml.XPathSelectElements("/HattrickData/Match/Bookings/Booking"))
        //        ret.Add(Booking.FromXml(bookingNode));
        //    return ret;
        //}

        public override string ToString()
        {
            return base.ToString() + " Detailed";
        }

        public void ReplaceEvents(IEnumerable<MatchEvent> distinctEvents)
        {
            Events = new List<MatchEvent>(distinctEvents);
        }
    }
}

// TODO: make them all part of the event class?
//public class Goal
//{
//    public int Index { get; private set; }
//    public int PlayerID { get; private set; }
//    public string PlayerName { get; private set; }
//    public int ScorerTeamID { get; private set; }
//    public int HomeGoals { get; private set; }
//    public int AwayGoals { get; private set; }
//    public int MinuteScored { get; private set; }

//    private Goal() { }
//    internal static Goal FromXml(XElement goal)
//    {
//        HattrickXmlReader reader = new HattrickXmlReader(goal);
//        return new Goal()
//        {
//            Index = reader.ReadAttributeNumber("Index"),
//            PlayerID = reader.ReadNumber("ScorerPlayerID"),
//            PlayerName = reader.ReadString("ScorerPlayerName"),
//            ScorerTeamID = reader.ReadNumber("ScorerTeamID"),
//            HomeGoals = reader.ReadNumber("ScorerHomeGoals"),
//            AwayGoals = reader.ReadNumber("ScorerAwayGoals"),
//            MinuteScored = reader.ReadNumber("ScorerMinute")
//        };
//    }
//    public override string ToString()
//    {
//        return string.Format("{0} Scores the #{1} Goal In The {2} Minute, Making It {3}-{4} !", PlayerName, (Index + 1), MinuteScored, HomeGoals, AwayGoals);
//    }
//}


//public class Booking
//{
//    public int Index { get; private set; }
//    public int PlayerID { get; private set; }
//    public string PlayerName { get; private set; }
//    public int BookingTeamID { get; private set; }
//    public BookingType BookingType { get; private set; }
//    public int MinuteBooked { get; private set; }

//    private Booking() { }
//    internal static Booking FromXml(XElement booking)
//    {
//        HattrickXmlReader reader = new HattrickXmlReader(booking);
//        return new Booking()
//        {
//            Index = reader.ReadAttributeNumber("Index"),
//            PlayerID = reader.ReadNumber("BookingPlayerID"),
//            PlayerName = reader.ReadString("BookingPlayerName"),
//            BookingTeamID = reader.ReadNumber("BookingTeamID"),
//            BookingType = reader.ReadBookingType("BookingType"),
//            MinuteBooked = reader.ReadNumber("BookingMinute")
//        };
//    }
//    public override string ToString()
//    {
//        switch (BookingType)
//        {
//            case BookingType.YellowCard:
//                return string.Format("{0} Got Booked At The {1} Minute", PlayerName, MinuteBooked);
//            case BookingType.RedCard:
//                return string.Format("{0} Got Sent Off At The {1} Minute", PlayerName, MinuteBooked);
//            default:
//                return "Unknown Details";
//        }
//    }
//}

